package com.app.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeHelper {
    private static String CurrentTime;
    private static String CurrentDate;

    public static String getCurrentYear() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(NowDate);
    }

    public static String getCurrentMonth() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(NowDate);
    }

    public static String getCurrentDay() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(NowDate);
    }

    public static String getCurrentTime() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static Date getCurrentDateTime() {
        final Date NowDate = new Date();
        return NowDate;
    }

    public static String getCurrentCompactTime() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static String getCurrentCompactTime1() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return TimeHelper.CurrentTime = formatter.format(NowDate);
    }

    public static String getYesterdayCompactTime() {
        final Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return TimeHelper.CurrentTime = formatter.format(cal.getTime());
    }

    public static String getYesterdayCompactTimeForFileName() {
        final Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        System.out.print(cal.getTime());
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return TimeHelper.CurrentTime = formatter.format(cal.getTime());
    }

    public static String getCurrentDate() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentDate1() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentFirstDate() {
        final Date NowDate = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        return TimeHelper.CurrentDate = formatter.format(NowDate);
    }

    public static String getCurrentLastDate() {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = null;
        try {
            final Date date = formatter.parse(getCurrentFirstDate());
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(2, 1);
            calendar.add(6, -1);
            return formatDate(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String formatDate(final Date date) {
        return formatDateByFormat(date, "yyyy-MM-dd");
    }

    public static String formatDateByFormat(final Date date, final String format) {
        String result = "";
        if (date != null) {
            try {
                final SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    public static String addDay(final String currentdate, final int add_day) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final int year = Integer.parseInt(currentdate.substring(0, 4));
            final int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(5, add_day);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String addMonth(final String currentdate, final int add_month) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final int year = Integer.parseInt(currentdate.substring(0, 4));
            final int month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdate.substring(8, 10));
            gc = new GregorianCalendar(year, month, day);
            gc.add(2, add_month);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int monthDiff(final String beforeTime, final String endTime) {
        if (beforeTime == null || endTime == null) {
            return 0;
        }
        try {
            final int beforeYear = Integer.parseInt(beforeTime.substring(0, 4));
            final int endYear = Integer.parseInt(endTime.substring(0, 4));
            final int beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1;
            final int endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1;
            return (endYear - beforeYear) * 12 + (endMonth - beforeMonth);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String addMinute(final String currentdatetime, final int add_minute) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            final int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            final int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, add_minute);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String addSecond(final String currentdatetime, final int add_second) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(11, 13));
            final int minute = Integer.parseInt(currentdatetime.substring(14, 16));
            final int second = Integer.parseInt(currentdatetime.substring(17, 19));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(13, add_second);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Long getTimeInMillis(final String currentdatetime) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.util.Date date = df.parse(currentdatetime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            long timestamp = cal.getTimeInMillis();
            return timestamp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static Long getTimeInMillis1(final String currentdatetime) {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date date = df.parse(currentdatetime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            long timestamp = cal.getTimeInMillis();
            return timestamp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String addMinute1(final String currentdatetime, final int add_minute) {
        GregorianCalendar gc = null;
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            final int year = Integer.parseInt(currentdatetime.substring(0, 4));
            final int month = Integer.parseInt(currentdatetime.substring(5, 7)) - 1;
            final int day = Integer.parseInt(currentdatetime.substring(8, 10));
            final int hour = Integer.parseInt(currentdatetime.substring(8, 10));
            final int minute = Integer.parseInt(currentdatetime.substring(8, 10));
            final int second = Integer.parseInt(currentdatetime.substring(8, 10));
            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(12, add_minute);
            return formatter.format(gc.getTime());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date parseDate(final String sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            final Date date = bartDateFormat.parse(sDate);
            return date;
        } catch (Exception ex) {
            return null;
        }
    }

    public static String parseDate(final Date sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String format = bartDateFormat.format(sDate);
            return format;
        } catch (Exception ex) {
            return null;
        }
    }

    public static String parseDate1(final Date sDate) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String format = bartDateFormat.format(sDate);
            return format;
        } catch (Exception ex) {
            return null;
        }
    }

    public static Date parseDateTime(final String sDateTime) {
        final SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            final Date date = bartDateFormat.parse(sDateTime);
            return date;
        } catch (Exception ex) {
            return null;
        }
    }

    public static int getTotalDaysOfMonth(final String strDate) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final Calendar calendar = new GregorianCalendar();
        Date date = new Date();
        try {
            date = sdf.parse(strDate);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        calendar.setTime(date);
        final int month = calendar.get(2) + 1;
        final int day = calendar.getActualMaximum(5);
        return day;
    }

    public static long getDateSubDay(final String startDate, final String endDate) {
        final Calendar calendar = Calendar.getInstance();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long theday = 0L;
        try {
            calendar.setTime(sdf.parse(startDate));
            final long timethis = calendar.getTimeInMillis();
            calendar.setTime(sdf.parse(endDate));
            final long timeend = calendar.getTimeInMillis();
            theday = (timethis - timeend) / 86400000L;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theday;
    }

    public static int getCurrentWeek() {
        final Calendar cal = Calendar.getInstance();
        final int week = 7;
        if (cal.get(7) != 1) {
            return cal.get(7) - 1;
        }
        return week;
    }

    public static long getTimestamp(String time, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = df.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long s = date.getTime();
        return s / 1000;
    }

    public static String calFullDate(String time, String pattern) {

        SimpleDateFormat sd = new SimpleDateFormat(pattern);
        String format = null;
        try {
            Date parse = sd.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);
            cal.add(Calendar.SECOND, +5);

            format = sd.format(cal.getTime());


        } catch (ParseException e) {
            e.printStackTrace();
        }

        return format;
    }

    public static Long subTime(String startTime, String endTime) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime = 0;
        long OTime = 0;
        try {
            NTime = df.parse(startTime).getTime();
            OTime = df.parse(endTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //从对象中拿到时间

        long diff = (OTime - NTime);
        return diff / 1000;
    }

    public static String formatZlrq(String zlrq) {
        if (zlrq == null || zlrq.isEmpty()) {
            return zlrq;
        }
        
        // 如果已经符合格式，则直接返回
        if (zlrq.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
            return zlrq;
        }
        
        // 处理带微秒的时间格式 2025-06-22 02:16:50.000000
        if (zlrq.contains(".")) {
            return zlrq.substring(0, zlrq.indexOf('.'));
        }
        
        return zlrq;
    }

    public static void main(final String[] args) {

        System.out.println(subTime("2020-11-25 00:00:00", "2020-11-25 00:01:00"));


    /*    long l = calFullDate("2020-11-25", "yyyy-MM-dd");
        System.out.println(l);*/
    }
}
